/**
 * Created by Eoly on 2017/1/1.
 */
function Enemy() {
    this.id = 1;

    //需要外部设置
    this.enemyCanvas = null;

    this.wave = [];
    this.frequencyWaving = 1000;
    this.waveCount = 0;
    this.difficulty = 0;
    this.wavingHandle = 0;
    this.levelPoint = 20;


    this.init = function () {
        var enemyPlane = {
            multiple: 1,
            Obj: EnemyPlane
        };
        this.wave.push(enemyPlane);
    };
    this.init();
}

Enemy.prototype = {
    constructor: Enemy,


    makeDifficulty: function (plane) {

        var _this = this;


        switch (this.difficulty) {
            case 0:
                return function () {
                    plane.setPlayerBelongs(_this);
                    plane.setCentrePoint(new Point((0.8 * Math.random() + 0.1) * window.innerWidth, 10));
                    plane.setPlaneCanvas(_this.enemyCanvas);
                    plane.setBulletCanvas(_this.enemyCanvas);
                };
            case 1:
                return function () {
                    plane.setPlayerBelongs(_this);
                    plane.setCentrePoint(new Point((0.8 * Math.random() + 0.1) * window.innerWidth, 10));
                    plane.setPlaneCanvas(_this.enemyCanvas);
                    plane.setBulletCanvas(_this.enemyCanvas);
                    plane.startShoot();
                };
            case 2:
                return function () {
                    var EnemyPack = function () {
                        EnemyNormalPack.call(this);
                        this.frequencyFiring = 2000;
                    };
                    Extend(EnemyNormalPack, EnemyPack);
                    EnemyPack.prototype.setBullets = function (bullet, i) {
                        bullet.modelUsed.isFaceDeg = true;
                        var p1 = this.centrePoint;
                        var p2 = player.plane != null ? player.plane.centrePoint : new Point(0, 100);
                        bullet.runningTrack.direction = p1.getDegFromAnotherPoint(p2);
                        bullet.runningTrack.speed = 2;
                    };
                    plane.mainWeaponPack = new EnemyPack();
                    plane.setPlayerBelongs(_this);
                    plane.setCentrePoint(new Point((0.8 * Math.random() + 0.1) * window.innerWidth, 10));
                    plane.setPlaneCanvas(_this.enemyCanvas);
                    plane.setBulletCanvas(_this.enemyCanvas);
                    plane.startShoot();
                };
            case 3:
                return function () {
                    var EnemyPack = function () {
                        EnemyNormalPack.call(this);
                        this.frequencyFiring = 2000;
                    };
                    Extend(EnemyNormalPack, EnemyPack);
                    EnemyPack.prototype.setBullets = function (bullet, i) {
                        bullet.modelUsed.isFaceDeg = true;
                        bullet.runningTrack.speed = 2;
                        bullet.collisionDamage = 600;
                        var p1 = this.centrePoint;
                        var p2 = player.plane != null ? player.plane.centrePoint : new Point(0, 100);
                        bullet.runningTrack.direction = p1.getDegFromAnotherPoint(p2);
                    };
                    plane.mainWeaponPack = new EnemyPack();
                    plane.setPlayerBelongs(_this);
                    plane.setCentrePoint(new Point((0.8 * Math.random() + 0.1) * window.innerWidth, 10));
                    plane.setPlaneCanvas(_this.enemyCanvas);
                    plane.setBulletCanvas(_this.enemyCanvas);
                    plane.startShoot();
                };
            case 4:
                return function () {
                    plane.mainWeaponPack = new EnemyFallowPack();
                    plane.setPlayerBelongs(_this);
                    plane.setCentrePoint(new Point((0.8 * Math.random() + 0.1) * window.innerWidth, 10));
                    plane.setPlaneCanvas(_this.enemyCanvas);
                    plane.setBulletCanvas(_this.enemyCanvas);
                    plane.startShoot();
                };
            case 5:
                return function () {
                    var EnemyPack = function () {
                        EnemyFallowPack.call(this);
                    };
                    Extend(EnemyFallowPack, EnemyPack);
                    EnemyPack.prototype.setBullets = function (bullet, i) {
                        bullet.modelUsed.isFaceDeg = true;
                        bullet.runningTrack.sensibility = 1.2;
                    };
                    plane.mainWeaponPack = new EnemyPack();
                    plane.setPlayerBelongs(_this);
                    plane.setCentrePoint(new Point((0.8 * Math.random() + 0.1) * window.innerWidth, 10));
                    plane.setPlaneCanvas(_this.enemyCanvas);
                    plane.setBulletCanvas(_this.enemyCanvas);
                    plane.startShoot();
                };
            default :
                return function () {
                    var EnemyPack = function () {
                        EnemyFallowPack.call(this);
                    };
                    Extend(EnemyFallowPack, EnemyPack);
                    EnemyPack.prototype.setBullets = function (bullet, i) {
                        bullet.modelUsed.isFaceDeg = true;
                        bullet.runningTrack.sensibility = 0.5 + (_this.difficulty - 5) * 0.1;
                        bullet.mark += (_this.difficulty - 5) * 10;
                        bullet.collisionDamage += (_this.difficulty - 5) * 10;
                    };
                    plane.mark = 200 + (_this.difficulty - 5) * 50;
                    plane.mainWeaponPack = new EnemyPack();
                    plane.setPlayerBelongs(_this);
                    plane.setCentrePoint(new Point((0.8 * Math.random() + 0.1) * window.innerWidth, 10));
                    plane.setPlaneCanvas(_this.enemyCanvas);
                    plane.setBulletCanvas(_this.enemyCanvas);
                    plane.startShoot();
                };
        }
    },

    waveRunning: function () {
        this.waveCount = 1 + this.waveCount;
        player.mark += 10 * this.difficulty;
        for (var i in this.wave) {
            for (var m = 0; m < this.wave[i].multiple; m++) {
                var plane = new this.wave[i].Obj;
                this.makeDifficulty(plane)();
            }
        }
        if (this.waveCount >= this.levelPoint) {
            this.changeDifficulty();
        }
    },

    waveStart: function () {
        var _this = this;
        _this.wavingHandle = setTimeout(function () {
            _this.waveRunning();
            requestAnimationFrame(function () {
                _this.waveStart();
            });
        }, this.frequencyWaving)
    },


    changeDifficulty: function () {
        this.waveCount = 0;
        this.frequencyWaving -= this.levelPoint;
        this.levelPoint += 2;
        this.difficulty++;
    }
};